package com.manner.groupon.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.manner.groupon.common.BaseResponse;
import com.manner.groupon.common.ResultUtils;
import com.manner.groupon.model.entity.CarouselImage;
import com.manner.groupon.service.store.CarouselImageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Manner
 * @Description: 轮播图数据请求控制器
 * @DateTime: 2024/10/6 16:28
 */
@RestController
@RequestMapping("/ad")
@RequiredArgsConstructor
public class CarouseImageController {

    @Resource
    private CarouselImageService carouseImageService;


    @GetMapping
    public BaseResponse<List<CarouselImage>> list(@RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false, defaultValue = "0") Integer status) {
        QueryWrapper<CarouselImage> wrapper = new QueryWrapper<>();
        //判断
        if (!status.equals(0)) {
            wrapper.eq("status", status);
        }
        wrapper.like("title", title + '%')
                .orderByAsc("sort");
        //查询
        List<CarouselImage> list = carouseImageService.list(wrapper);
        return ResultUtils.success(list);
    }

    @PostMapping
    public BaseResponse<String> save(@RequestBody CarouselImage carouselImage) {
        carouseImageService.save(carouselImage);
        return ResultUtils.success("新增成功");
    }

    @PutMapping
    public BaseResponse<String> update(@RequestBody CarouselImage carouselImage) {
        carouseImageService.updateById(carouselImage);
        return ResultUtils.success("修改成功");
    }

    @GetMapping("/{id}")
    public BaseResponse<CarouselImage> queryById(@PathVariable Long id) {
        CarouselImage carouselImage = carouseImageService.getById(id);
        return ResultUtils.success(carouselImage);
    }

    @DeleteMapping("/{id}")
    public BaseResponse<String> del(@PathVariable Long id) {
        carouseImageService.removeById(id);
        return ResultUtils.success("删除成功");
    }

}
